package com.mall.shopping.services;

import com.alibaba.fastjson.JSON;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.services.cache.CacheManager;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * @author:tanxin Desc:
 * @date:2020/7/11 11:11
 */
@Slf4j
@Component
@Service
public class ProductDetailServiceImpl implements IProductService {
    @Resource
    ItemMapper itemMapper;
    @Resource
    ItemDescMapper itemDescMapper;
    @Resource
    CacheManager cacheManager;
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        Item item = itemMapper.selectByPrimaryKey(request.getId());
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
       // request.requestCheck();
        //查询是否有缓存
        String json = cacheManager.checkCache(generatorProduceCacheKey(request));
        if(StringUtils.isNotBlank(json)){
            ProductDetailDto productDetailDto = JSON.parseObject(json, ProductDetailDto.class);
            cacheManager.expire(generatorProduceCacheKey(request),GlobalConstants.PRODUCT_ITEM_EXPIRE_TIME);
            response.setProductDetailDto(productDetailDto);
            return response;
        }
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(item.getId());
        ProductDetailDto productDetailDto = new ProductDetailDto();
        productDetailDto.setProductId(item.getId());
        //另一张表中
        productDetailDto.setDetail(itemDesc.getItemDesc());
        //limitNum为Integer类型
        productDetailDto.setLimitNum(item.getLimitNum().longValue());
        productDetailDto.setProductImageBig(item.getImageBig());
        productDetailDto.setSalePrice(item.getPrice());
        productDetailDto.setProductName(item.getTitle());
        productDetailDto.setSubTitle(item.getSellPoint());
        //小图
        productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
        response.setProductDetailDto(productDetailDto);
        //设置缓存
        cacheManager.setCache(generatorProduceCacheKey(request),JSON.toJSON(productDetailDto).toString(),
                GlobalConstants.PRODUCT_ITEM_EXPIRE_TIME);
        return response;
    }
    //产生缓存key
    private String generatorProduceCacheKey(ProductDetailRequest request){
        StringBuilder stringBuilder = new StringBuilder(GlobalConstants.PRODUCT_ITEM_CACHE_KEY);
        stringBuilder.append(":").append(request.getId());
        return stringBuilder.toString();
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        return null;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        return null;
    }
}
